import { ref, reactive, nextTick, onMounted } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from 'vue-router';
import { QueryRatingCustomerInfo, LookInfo, IsLooked } from "@/api/stu/bankBigData";

export function useLookCustomer() {
  const route = useRoute();
  const dataList = ref<any>([]);
  const dataList2 = ref<any>([]);
  const loading = ref<boolean>(false);
  const loading2 = ref<boolean>(false);
  const isLook = ref(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const pagination2 = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 120
    },
    {
      label: "公司名称",
      prop: "companyName",
      minWidth: 200,
    },
    {
      label: "法人姓名",
      prop: "legalPersonName",
      minWidth: 100,
    },
    {
      label: "法人证件号",
      prop: "corporateIdNumber",
      minWidth: 100
    },
    {
      label: "法人联系方式",
      prop: "legalPersonContact",
      minWidth: 200,
    },

    {
      label: "所属行业",
      prop: "industry",
      minWidth: 150,
    },
    {
      label: "成立时间",
      prop: "establishmentTime",
      minWidth: 150
    },
    {
      label: "公司规模",
      prop: "companySize",
      minWidth: 100
    },
    {
      label: "竞争优势",
      prop: "competitiveAdvantage",
      minWidth: 200
    },
    {
      label: "管理层素质",
      prop: "managementQuality",
      minWidth: 200
    },
    {
      label: "信用记录",
      prop: "creditHistory",
      minWidth: 150
    },
    {
      label: "逾期情况",
      prop: "overdueSituation",
      minWidth: 150,
    },
    {
      label: "每股净资产（元）",
      prop: "netAssetsPerShare",
      minWidth: 150,
    },
    {
      label: "总资产利润率（%）",
      prop: "totalAssetProfitMargin",
      minWidth: 150
    },
    {
      label: "销售毛利率（%）",
      prop: "salesGrossProfitMargin",
      minWidth: 200,
    },

    {
      label: "流动比率（%)",
      prop: "currentRatio",
      minWidth: 150,
    },
    {
      label: "速动比率(%)",
      prop: "quickRatio",
      minWidth: 150
    },
    {
      label: "净利润增长率(%)",
      prop: "netProfitGrowthRate",
      minWidth: 200
    },
    {
      label: "应收账款周转天数(天)",
      prop: "daysAccountsReceivableTurnover",
      minWidth: 200
    },
    {
      label: "固定资产周转率(%)",
      prop: "fixedAssetTurnoverRate",
      minWidth: 200
    },
    {
      label: "存货周转天数(天)",
      prop: "inventoryTurnoverDays",
      minWidth: 200
    },
    {
      label: "发展前景",
      prop: "developmentProspects",
      minWidth: 150
    },
    {
      label: "公司结构",
      prop: "companyStructure",
      minWidth: 100,
    },
    {
      label: "经营管理",
      prop: "companyStructure",
      minWidth: 100,
    },
    {
      label: "偿债能力",
      prop: "debtPayingAbility",
      minWidth: 100
    },
    {
      label: "财务管理",
      prop: "financialManagement",
      minWidth: 200,
    },

    {
      label: "行业前景",
      prop: "industryProspects",
      minWidth: 150,
    },
    {
      label: "是否有国家扶持",
      prop: "isHaveNationalSupport",
      minWidth: 150
    },
    {
      label: "是否有地方扶持",
      prop: "isHaveLocalSupport",
      minWidth: 150
    },
    {
      label: "同行认可度",
      prop: "peerRecognition",
      minWidth: 200
    },
    {
      label: "企业声誉",
      prop: "corporateReputation",
      minWidth: 200
    },
    {
      label: "违规频率",
      prop: "violationFrequency",
      minWidth: 150
    },

  ];

  const columns2: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    },
    {
      label: "姓名",
      prop: "userName",
      minWidth: 100,
    },
    {
      label: "性别",
      prop: "gender",
      minWidth: 100
    },
    {
      label: "年龄",
      prop: "age",
      minWidth: 100
    },
    {
      label: "平均月收入",
      prop: "avgMonthlyIncome",
      minWidth: 200,
    },

    {
      label: "配偶平均月收入",
      prop: "spouseAvgMonthlyIncome",
      minWidth: 150,
    },
    {
      label: "收入来源",
      prop: "sourceIncome",
      minWidth: 150
    },
    {
      label: "收入稳定性",
      prop: "incomeStability",
      minWidth: 100
    },
    {
      label: "财务状况",
      prop: "financialCondition",
      minWidth: 200
    },
    {
      label: "资产规模",
      prop: "assetSize",
      minWidth: 200
    },
    {
      label: "借贷历史",
      prop: "loanHistory",
      minWidth: 150
    },
    {
      label: "借贷信用",
      prop: "loanCredit",
      minWidth: 100,
    },
    {
      label: "欠款情况",
      prop: "debtSituation",
      minWidth: 100,
    },
    {
      label: "风险偏好",
      prop: "riskAppetite",
      minWidth: 100
    },
    {
      label: "承受能力",
      prop: "affordability",
      minWidth: 200,
    },

    {
      label: "工作稳定性",
      prop: "workStability",
      minWidth: 150,
    },
    {
      label: "职业背景",
      prop: "careerBackground",
      minWidth: 150
    },
    {
      label: "居住稳定性",
      prop: "residentialStability",
      minWidth: 100
    },
    {
      label: "所属行业前景",
      prop: "industryProspects",
      minWidth: 200
    },
    {
      label: "是否在黑名单",
      prop: "isBlacklist",
      minWidth: 200
    },
    {
      label: "是否有恶意透支行为",
      prop: "isHaveOverdraftBehavior",
      minWidth: 150
    },
    {
      label: "征信查询频率",
      prop: "creditInquiryFrequency",
      minWidth: 100,
    },
    {
      label: "个人竞争力",
      prop: "personalCompetitiveness",
      minWidth: 100,
    },
    {
      label: "家庭条件",
      prop: "familyConditions",
      minWidth: 100
    },

  ];



  // 分页
  function handleSizeChange(val) {
    getCompanyCustomerList()
  }

  function handleCurrentChange(val) {
    getCompanyCustomerList()
  }

  // 分页
  function handleSizeChanges(val) {
    getRetailCustomerList()
  }

  function handleCurrentChanges(val) {
    getRetailCustomerList()
  }

  /* 模块2 end*/

  // 获取企业征信数据系统
  async function getCompanyCustomerList() {
    loading.value = true
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      detailId: '1',
    };
    const res = await QueryRatingCustomerInfo(data);
    if (res.success) {
      pagination.total = res.data.totalCount;
      dataList.value = res.data.items;
      loading.value = false
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false }, 1500)
    }
  }

  // 获取企业征信数据系统
  async function getRetailCustomerList() {
    loading2.value = true
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      pageIndex: pagination2.currentPage,
      pageCount: pagination2.pageSize,
      detailId: '',
    };
    const res = await QueryRatingCustomerInfo(data);
    if (res.success) {
      pagination2.total = res.data.totalCount;
      dataList2.value = res.data.items;
      loading2.value = false
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading2.value = false }, 1500)
    }
  }

  function ShowList() {
    getCompanyCustomerList()
    getRetailCustomerList()
  }

  onMounted(() => {
    handleIsLOOk()
  })
  async function handleIsLOOk() {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      module: 1
    }
    let res = await IsLooked(data)
    if (res.data) {
      isLook.value = true
      ShowList()
    }
  }

  async function isLookChange() {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      module: 1
    }
    let res = await LookInfo(data)
    if (res.success) {
      isLook.value = true
      ShowList()
    } else {
      ElMessage.error(res.msg)
    }
  }


  return {
    dataList,
    dataList2,
    loading,
    loading2,
    pagination,
    pagination2,
    columns,
    columns2,
    ShowList,
    handleSizeChange,
    handleCurrentChange,
    handleSizeChanges,
    handleCurrentChanges,
    isLook,
    isLookChange
  }
}
